import random

import requests
import logging
import config


class ZhanSuoAPI:

    # 文件上传
    @classmethod
    def upload_file(cls, token, f):
        url = config.monitor_domain + "/cms/file/uploadFile"
        headers = {"Authorization": f"Bearer {token}"}
        resp = requests.post(url=url, headers=headers, files={"files": f})
        logging.info(f"upload_file url:{url}")
        # logging.info(f"upload_file resp: {resp.text}")
        return resp

    #添加站所
    @classmethod
    def add_device(cls,token,body):
        url = config.monitor_domain+"/cm/device/addDevice"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers = {"Authorization": token, "Uuidstr": uuid}
        resp =requests.post(url=url,headers=headers,json=body)
        logging.info(f"add_device url:{url}")
        logging.info(f"add_device resp: {resp.text}")
        return resp

    # 删除站所 根据站所id
    @classmethod
    def remove_DeviceByIds(cls,token,deviceIds):
        url = config.monitor_domain + f"/cm/device/removeDeviceByIds?deviceIds={deviceIds}"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers = {"Authorization": token, "Uuidstr": uuid}
        resp = requests.get(url=url, headers=headers)
        logging.info(f"remove_DeviceByIds url:{url}")
        logging.info(f"remove_DeviceByIds resp: {resp.text}")
        return resp

    #编辑站所
    @classmethod
    def edit_device(cls,token,body):
        url = config.monitor_domain + "/cm/device/editDevice"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers = {"Authorization": token, "Uuidstr": uuid,"content-type":"application/json"}
        resp = requests.put(url=url, headers=headers, json=body)
        logging.info(f"edit_device url:{url}")
        logging.info(f"edit_device resp: {resp.text}")
        return resp

        pass
    #查看站所
    @classmethod
    def check_device(cls,token,id):
        url = config.monitor_domain + f"/cm/device/{id}"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers = {"Authorization": token, "Uuidstr": uuid}
        resp = requests.get(url=url, headers=headers)
        logging.info(f"check_device url:{url}")
        logging.info(f"check_device resp: {resp.text}")
        return resp

    # 根据站点名字查看站所
    @classmethod
    def query_device_list(cls, token,deviceName):
        url = config.monitor_domain + "/cm/device/deviceList"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers = {"Authorization": token, "Uuidstr": uuid}
        params = {"deviceName":deviceName}
        resp = requests.get(url=url, headers=headers,params=params)
        logging.info(f"query_device_list url:{url}")
        logging.info(f"query_device_list resp: {resp.text}")
        return resp

        # 根据站点名字查看站所

    @classmethod
    def query_deptCodeList(cls, token):
        url = config.monitor_domain + "/screen/deptCodeList"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers = {"Authorization": token, "Uuidstr": uuid}
        resp = requests.get(url=url, headers=headers)
        logging.info(f"query_deptCodeList url:{url}")
        logging.info(f"query_deptCodeList resp: {resp.text}")
        return resp
    #查看站所下面的关联的监控设备
    @classmethod
    def get_UnboundMonitorDeviceList(cls,token,stationId,deviceName='',deviceLargeTypeCode='',deviceType=''):
        url = config.monitor_domain + f"/device/monitorDeviceLink/getUnboundMonitorDeviceList?stationId={stationId}&deviceName={deviceName}&deviceLargeTypeCode={deviceLargeTypeCode}&deviceType={deviceType}&deviceId={stationId}"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers = {"Authorization": token, "Uuidstr": uuid}
        resp = requests.get(url=url, headers=headers)
        logging.info(f"query_zhansuo_monitor_equ url:{url}")
        logging.info(f"query_zhansuo_monitor_equ resp: {resp.text}")
        return resp

    #给站所关联监测设备
    @classmethod
    def add_DeviceMonitorDeviceLink(cls,token,body):
        url = config.monitor_domain + "/device/monitorDeviceLink/addDeviceMonitorDeviceLink"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers = {"Authorization": token, "Uuidstr": uuid}
        resp = requests.post(url=url, headers=headers, json=body)
        logging.info(f"add_DeviceMonitorDeviceLink url:{url}")
        logging.info(f"add_DeviceMonitorDeviceLink resp: {resp.text}")
        return resp


    #删除关联监测设备  deleteDeviceMonitorDeviceLink/1823959091060482049,1823959091064676353,1823959091064676354
    @classmethod
    def delete_DeviceMonitorDeviceLink(cls,token,ids):
        url = config.monitor_domain + f"/device/monitorDeviceLink/deleteDeviceMonitorDeviceLink/{ids}"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers = {"Authorization": token, "Uuidstr": uuid}
        resp = requests.delete(url=url, headers=headers)
        logging.info(f"delete_DeviceMonitorDeviceLink url:{url}")
        logging.info(f"delete_DeviceMonitorDeviceLink resp: {resp.text}")
        return resp

    #根据站点id获取关联的监测设备接口
    @classmethod
    def get_MonitorDeviceListByDeviceId(cls,token,stationId):
        url = config.monitor_domain + f"/device/monitorDeviceLink/getMonitorDeviceListByDeviceId/{stationId}"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers = {"Authorization": token, "Uuidstr": uuid}
        resp = requests.get(url=url, headers=headers)
        logging.info(f"get_MonitorDeviceListByDeviceId url:{url}")
        logging.info(f"get_MonitorDeviceListByDeviceId resp: {resp.text}")
        return resp
        pass








